package com.dragon.mmochat.message;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.model.enumType.MmochatGangLevelType;
import com.dragon.mmochat.model.enumType.MmochatGangMemberType;
import com.dragon.mmochat.model.enumType.MmochatGangSkillType;
import com.dragon.mmochat.model.gang.MmochatGang;
import com.dragon.mmochat.model.gang.MmochatGangMember;
import com.dragon.mmochat.model.gang.MmochatGangRequester;
import com.dragon.mmochat.model.gang.MmochatGangSkill;
import com.dragon.mmochat.service.MmochatMainService;
import com.skymobi.buffer.SkymobiBuffer;
import com.skymobi.message.SkymobiProtocolMessage;

public class Mmochat_15_2S extends SkymobiProtocolMessage {
	int hallState;
	MmochatGang gang;
	MmochatGangMemberType myType;

	public Mmochat_15_2S() {
		setMsgCode(MmochatConstant.MSGCODE_MMOCHAT_S_15_2);
	}

	@Override
	protected void encodeBody(SkymobiBuffer out) {
		out.putUnsignedShort(hallState);
		out.putInt(myType.getValue());

		// 基本信息
		int memberNum = gang.getMembers().size();
		out.putInt(gang.getId());
		out.putPrefixedString(gang.getName(), 1);
		out.putPrefixedString(gang.getLevel().toString());
		out.putShort((short) memberNum);
		out.putShort((short) gang.getLevel().getMaxMemberNum());
		out.putInt(gang.getMoney());
		out.putInt(gang.getBuildValue());
		out.putPrefixedString(gang.getInfo(), 2);

		// 帮派成员
		int showMemberNum = Math.min(memberNum,
				MmochatConstant.maxMemberToShowPerPage);
		List<MmochatGangMember> members = new ArrayList<MmochatGangMember>(gang
				.getMembers().values());
		Collections.sort(members, Collections.reverseOrder());
		int pageNum = (int) Math.ceil(1.0 * memberNum
				/ MmochatConstant.maxMemberToShowPerPage);

		out.putShort((short) pageNum);
		out.put((byte) showMemberNum);
		for (int i = 0; i < showMemberNum; i++) {
			MmochatGangMember member = members.get(i);
			out.putInt(member.getRoleId());
			out.putUnsignedShort(member.getBmpId());
			if (MmochatMainService.isPlayerOnline(member.getRoleId())) {
				out.put((byte) 1);
			} else {
				out.put((byte) 0);
			}
			out.putPrefixedString(member.getName(), 1);
			out.putPrefixedString(member.getType().toString(), 1);
			out.putInt(member.getBuildValue());
			out.putInt(member.getCurWeekBuildValue());
		}

		// 帮派技能
		out.put((byte) MmochatGangSkillType.values().length);
		for (MmochatGangSkillType skillType : MmochatGangSkillType.values()) {
			MmochatGangSkill skill = gang.getSkills().get(skillType);
			out.putInt(skillType.getValue());
			out.putPrefixedString(skillType.toString(), 1);
			int curLevel = 0;
			if (skill != null) {
				curLevel = skill.getLevel();
			}
			int maxLevel = skillType.getMaxLevel();
			out.putPrefixedString(curLevel + "/" + maxLevel, 1);
			MmochatGangLevelType gangLevelNeed = skillType
					.getGangLevelNeedToDevelopSkill(curLevel);
			out.putPrefixedString(gangLevelNeed.toString() + "帮派", 1);
			if (gang.getLevel().getValue() >= gangLevelNeed.getValue()) {
				out.putInt(skillType.getGangBuildNeedToDevelopSkill(curLevel));
			} else {
				out.putInt(-1);
			}
		}

		// 申请入帮人员
		if (myType.getValue() < MmochatGangMemberType.副帮.getValue()) {
			out.put((byte) 0);
		} else {
			int showRequestNum = Math.min(gang.getRequesters().size(),
					MmochatConstant.maxMemberToShowPerPage);
			out.put((byte) showRequestNum);
			int count = 0;
			for (MmochatGangRequester requester : gang.getRequesters().values()) {
				if (count >= showRequestNum){
					break;
				}
				out.putInt(requester.getRoleId());
				out.putUnsignedShort(requester.getBmpId());
				out.putPrefixedString(requester.getName(), 1);
				out.putInt(requester.getLevel());
				count++;
			}
		}
	}

	public void setHallState(int hallState) {
		this.hallState = hallState;
	}

	public void setGang(MmochatGang gang) {
		this.gang = gang;
	}

	public void setMyType(MmochatGangMemberType myType) {
		this.myType = myType;
	}

}
